package com.cx.bank.view;

import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;
import com.cx.bank.util.AccountOverDrawnException;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 20:07
 */
public class DepositMoneyView extends JFrame {
    private JButton confirm;
    private JLabel moneyLabel;
    public JTextField moneyText;
    private Double money;


    public DepositMoneyView(){
        building();
        addlisteners();
    }

    private void building() {
        setLayout(null);
        moneyLabel = new JLabel("存款金额");
        confirm = new JButton("确认");
        moneyText = new JTextField(20);

        confirm.setBounds(250, 500, 120, 25);
        moneyLabel.setBounds(150, 250, 120, 25);
        moneyText.setBounds(250, 250, 200, 30);

        add(confirm);
        add(moneyLabel);
        add(moneyText);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private void addlisteners() {
        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                money = Double.valueOf(moneyText.getText());
                try {
                    UserDaoFactory userDaoFactory = UserDaoFactory.getInstance();
                    BankDaoInterface bankDao = userDaoFactory.getBankDao();
                    ManagerImpl manager = ManagerImpl.getInstance();
                    manager.setBankDao(bankDao);
                    User user = BankView.getUser();

                    MyInVocationHandler handler = new MyInVocationHandler();
                    handler.setTarget(manager);
                    handler.setUserName(user.getUserName());
                    IManager proxy = (IManager)handler.getProxy();

//                    boolean deposit = manager.deposit(user.getUserName(), money);
                    boolean deposit = proxy.deposit(user.getUserName(), money);
                    if (deposit){
                        System.out.println("存款成功！");
                        //关闭当前页面
                        DepositMoneyView.this.dispose();
                    }else {
                        System.out.println("存款失败");
                    }
                } catch (AccountOverDrawnException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }


            }
        });
    }

    public static void main(String[] args) {
        DepositMoneyView depositMoneyView = new DepositMoneyView();
        depositMoneyView.show();
    }
}
